package com.xcity.game.activity.luckdraw;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.item.ItemTemplate;

import naga.x.App;
import naga.x.common.Interval;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;

/**
 * 超级大转盘
 * @author yang.li
 *
 */
@Excel(value = "activity.xlsx", sheet = 4)
public class BigLuckDrawTemplate extends Template implements Interval, Comparable<BigLuckDrawTemplate> {

	/** 数量(如果id为0,count>0,则count表示奖励积分) */
	protected int count;

	/** 抽中概率(整数) */
	@JSONField(serialize = false)
	protected int probability;
	@JSONField(serialize = false)
	protected ItemTemplate ref;
	
	public static int maxProbability;
	protected int weightStart, weight;
	
	// 虚拟一个双倍积分物品id
	public static final int ID_DOUBLE_POINTS = 999999;
	public static final String NAME_DOUBLE_POINTS = "双倍积分";

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getProbability() {
		return probability;
	}

	public void setProbability(int probability) {
		this.probability = probability * 10;
	}
	
	public ItemTemplate getRef() {
		return id <= 0 || id == ID_DOUBLE_POINTS || ref != null ? ref : (ref = App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, id));
	}

	public String getName() {
		if (id == ID_DOUBLE_POINTS) {
			return NAME_DOUBLE_POINTS;
		}
		return getRef() != null ? getRef().getName() : null;
	}
	
	public String getIcon() {
		return getRef() != null ? getRef().getIcon() : null;
	}
	
	public String getDesc() {
		return getRef() != null ? getRef().getDesc() : null;
	}


	@Override
	public void validate() throws TemplateException {
		weightStart = maxProbability;
		maxProbability += probability;
		weight = maxProbability;
		if (!hasId() && count > 0) {
			id = ID_DOUBLE_POINTS;
		}
	}

	@Override
	@JSONField(serialize = false)
	public int getMinVal() {
		return weightStart;
	}

	@Override
	@JSONField(serialize = false)
	public int getMaxVal() {
		return weight;
	}

	@Override
	public int compareTo(BigLuckDrawTemplate o) {
		return probability - o.probability;
	}
}
